<template>
  <div class="page-list-live">
    <PageList>
      <template #header>传感器管理</template>
      <template #filter>
        <ListFilter :form="topFilter" @search="handleSearch" @reset="handleReset"></ListFilter>
      </template>
      <template #tips>
        <el-button type="primary" icon="el-icon-document-add" @click="handleAdd">新建传感器</el-button>
      </template>
      <template #table>
        <ListTable ref="RefListTable" :loading="tableLoading" :table-data="tableData" :table-col="tableCol">
          <template slot="col1" slot-scope="scope">
            <el-button icon="el-icon-edit" :loading="scope.row.loadingEdit" size="mini" type="text" @click="handleRowEdit(scope.row)">编辑</el-button>
            <el-button :loading="scope.row.loadingDel" size="mini" type="text" icon="el-icon-delete" style="color: red;" @click="handleRowDel(scope.row)">删除</el-button>
          </template>
        </ListTable>
      </template>
    </PageList>
    <BindingSensor ref="BindingSensor" @success="afterEditSuccess"></BindingSensor>
  </div>
</template>

<script>
  import listMixin from '@/mixins/list'
  import BindingSensor from './components/BindingSensor.vue'
  import ListFilter from '@/components/ListFilter'
  import { sensorOne } from '@/api/api'
  export default {
    components: {
      BindingSensor,
      ListFilter
    },
    mixins: [listMixin],
    data() {
      return {
        topFilter: {
          name: { label: '基站名称', placeholder: '请输入基站名称' }
        },
        tableData: [],
        tableCol: [
          { prop: 'name', label: '设备名称' },
          {
            prop: 'sensorStationId', label: '基站名称',
            formatter(row, col, cell) {
              return row.sensorStation.name || '--'
            }
          },
          // { prop: '', label: '通道名称' },
          { prop: 'onlineStatus', label: '在线状态' },
          { prop: 'currentValue', label: '当前值' },
          {
            prop: 'valueUpdateTime', label: '更新时间', formatter: function (row, col, cell) {
              return this.$format(cell, true)
            }.bind(this)
          },
          { label: '操作', custom: 'col1' }
        ]
      }
    },
    methods: {
      // 请求列表数据
      async getList(query) {
        this.tableLoading = true
        try {
          let obj = query || { pageIndex: 1, pageSize: 10, orderBy: 'createdTime', orderByAscent: false }
          const { data: { items: resData, count, pageIndex } } = await sensorOne('post', obj, false)
          this.tableData = []
          this.tableData = resData.map(item => {
            item.loadingEdit = false
            item.loadingDel = false
            return item
          })
          this.$nextTick(() => {
            this.$refs['RefListTable'].update({ page: pageIndex, count })
            this.tableLoading = false
          })
        } catch (err) {
          this.tableLoading = false
          console.error('getList err: ', err)
        }
      },
      async afterEditSuccess() {
        await this.$router.push({ query: {} })
        this.getList()
      },
      handleAdd() {
        this.$refs['BindingSensor'].open()
        this.$refs['BindingSensor'].getSensorStationList()
      },
      handleRowEdit(row) {
        this.$refs['BindingSensor'].open(row)
        this.$refs['BindingSensor'].getSensorStationList()
      },
      async delRowReq(row) {
        try {
          let obj = { id: row.id, value: null }
          await sensorOne('delete', obj, true)
          row.loadingDel = false
          await this.$router.push({ query: {} })
          this.getList()
          this.$message({
            type: 'success',
            message: '删除成功'
          })
        } catch (err) {
          row.loadingDel = false
          this.$message({
            type: 'error',
            message: '删除失败'
          })
          console.error('handleRowDel err: ', err)
        }
      },
      handleRowDel(row) {
        row.loadingDel = true
        this.$_delConfirm(
          '传感器',
          () => {
            this.delRowReq(row)
          },
          () => {
            row.loadingDel = false
          }
        )
      }
    }
  }
</script>

<style lang="scss" scoped>
</style>
